function addMapping() {
    var pidEle = $("pid");
    var ebidEle = $("ebid");
    var priorEle = $("priority");
    if (pidEle && ebidEle && priorEle) {
        var pid = pidEle.value;
        if (!pid) {
            alert("文章id不能为空:)");
            return;
        }
        var ebid = ebidEle.value;
        if (!ebid) {
            alert("电子书id不能为空:)");
            return;
        }
        var priority = priorEle.value;
        if (!priority) {
            alert("映射优先级不能为空:)");
            return;
        }
        var url = "/admin/ebookmapping";
        var callback = function(respJson) {
            if (respJson) {
                if (!respJson.errCode) {
                    //insertNewMapping(respJson);
                    location.reload();
                }
                else {alert("出错了：" + respJson.errMsg);}
            }
            else {alert("出错了：未知错误");}
        };
        var options = {
            values: {op:"new", pid:pid, ebid:ebid, priority:priority},
            errorHandler: function(status, statusText) {alert("出错了：服务器返回" + statusText);},
            timeout: 3000,
            timeoutHandler: function() {alert("出错了：连接服务器超时！");}
        };
        HTTP.post(url, callback, options);
    }
}

function delMapping(mappingId) {
    if (!confirm("你真的想删除这条映射吗？")) {return;}
    var mappingItem = $("mapping" + mappingId);
    if (mappingItem) {
        var url = "/admin/ebookmapping";
        var callback = function(respJson) {
            if (respJson) {
                if (!respJson.errCode) {
                    if (mappingItem.parentNode) {
                        mappingItem.parentNode.removeChild(mappingItem);
                    }
                }
                else {alert("出错了：" + respJson.errMsg);}
            }
            else {alert("出错了：未知错误");}
        };
        var options = {
            values: {op:"del", id:mappingId},
            errorHandler: function(status, statusText) {alert("出错了：服务器返回" + statusText);},
            timeout: 3000,
            timeoutHandler: function() {alert("出错了：连接服务器超时！");}
        };
        HTTP.post(url, callback, options);
    }
}

/* not correct yet */
function insertNewMapping(respJson) {
    var mappingTab = $("mappingTab");
    if (mappingTab) {
        var tbody = mappingTab.firstChild;
        var mappingNew = $("mappingNew");
        if (tbody && mappingNew) {
            var newTr = document.createElement("tr");
            newTr.id = "mapping" + respJson.id;
            newItemMarkup = "<td><a target=\"_blank\" href=\"/mathml/readpaper?pid=" +
                respJson.pid + "\">" + respJson.paperTitle + "</a></td>" +
                    "<td><a target=\"_blank\" href=\"/admin/ebookinfo?ebid=" +
                    respJson.ebid + "\">" + respJson.ebookTitle + "</a></td>" +
                    "<td>" + respJson.priority + "</td><td><a href=\"#\">" +
                    "删除这条对应关系</a></td>";
            newTr.innerHTML = newItemMarkup;
            tbody.insertBefore(newTr, mappingNew);
            delAction = newTr.lastChild.lastChild;
            ZTL.addEventListener(delAction, "click", function(){
                delMapping(respJson.id);
            });
        }
    }
}